package com.yksj.healthtalk.adapter;

import java.util.List;

import org.universalimageloader.core.ImageLoader;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.yksj.healthtalk.entity.CustomerInfoEntity;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.ui.app.AppData;
import com.yksj.healthtalk.ui.home.MyFriendListActivity.OnFrindListClickListener;

public class FriendListAdapter extends BaseAdapter{
	
	private final List<CustomerInfoEntity> list;
	private final List<CustomerInfoEntity> mAttentionList;
	private final LayoutInflater inflater;
	private Context context;
	private final OnFrindListClickListener mClickListener;
	private final ImageLoader mImageLoader;
	private final Drawable maleDrawable,femalDrawable,attenDrawable,notAttenDrawable;
	private int typeInt;
	public FriendListAdapter(Context context,String type,AppData mAppData,OnFrindListClickListener listener) {
		this.context = context;
		this.mAttentionList = mAppData.getInterestFriendList();
		this.list = (List<CustomerInfoEntity>)mAppData.getChatList(type);
		
		this.mClickListener = listener;
		this.typeInt=Integer.parseInt(type);
		inflater = LayoutInflater.from(context);
		mImageLoader = ImageLoader.getInstance();
			
	
		attenDrawable = context.getResources().getDrawable(R.drawable.attention);
		notAttenDrawable = context.getResources().getDrawable(R.drawable.not_attention);
		femalDrawable = context.getResources().getDrawable(R.drawable.women);
		maleDrawable = context.getResources().getDrawable(R.drawable.man);
	}

	@Override
	public int getCount() {
		return list.size();
	}

	
	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}
	

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final ViewHolder holder;
		final CustomerInfoEntity mCustomerInfoName = list.get(position);
		if (convertView == null){
			holder = new ViewHolder();  
			convertView = inflater.inflate(R.layout.friendlist_iterm, null);
			holder.headerImage = (ImageView) convertView.findViewById(R.id.head_image);
			holder.sexImage = (ImageView) convertView.findViewById(R.id.head_sex);
			holder.isDoctorImage = (ImageView) convertView.findViewById(R.id.v);
			holder.nameTxtV = (TextView) convertView.findViewById(R.id.name);
			holder.noteTxtV = (TextView) convertView.findViewById(R.id.note);
			holder.mesgeTxtV = (TextView) convertView.findViewById(R.id.item_dele);
			holder.isFriend=(ImageView) convertView.findViewById(R.id.is_friend);
			holder.isFriend.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					if(mClickListener != null)mClickListener.onClick(typeInt,mCustomerInfoName,true);
					notifyDataSetChanged();
				}
			});
			holder.headerImage.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					if(mClickListener != null)mClickListener.onClick(typeInt,mCustomerInfoName,false);
				}
			});
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		mImageLoader.displayImage(mCustomerInfoName.getSex(),mCustomerInfoName.getNormalHeadIcon(), holder.headerImage);
		
		if(!mAttentionList.contains(mCustomerInfoName)){//判断是否为我关注的对象
			holder.isFriend.setImageDrawable(attenDrawable);
		}else{
			holder.isFriend.setImageDrawable(notAttenDrawable);
		}
		
		if(mCustomerInfoName.isShow() || mCustomerInfoName.isAudit()){//是否为医师
			holder.isDoctorImage.setVisibility(View.VISIBLE);
		}else{
			holder.isDoctorImage.setVisibility(View.INVISIBLE);
		}
		
		holder.nameTxtV.setText(mCustomerInfoName.getName()+"("+mCustomerInfoName.getUsername()+")");
		holder.noteTxtV.setText(mCustomerInfoName.getCusMessag());
		
		if(!"".equals(mCustomerInfoName.getMessgeNumber())&&!"0".equals(mCustomerInfoName.getMessgeNumber())){
			holder.mesgeTxtV.setText(mCustomerInfoName.getMessgeNumber());
		}else{
			holder.mesgeTxtV.setVisibility(View.GONE);
		}
		return convertView;
	}
	
	static class ViewHolder {
		ImageView isFriend;//是否为我关注的好友
		ImageView isDoctorImage;
		TextView mesgeTxtV; //未读消息
		ImageView headerImage; // 头像
		ImageView sexImage; // 性别
		TextView nameTxtV; // 名称
		TextView noteTxtV; //简介
		TextView moreTxtV;
	}
}
